﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace YF_Music.Core.Behavior
{
  public static class ElementSizeMonitor
  {
    public static readonly DependencyProperty TrackHeightProperty =
         DependencyProperty.RegisterAttached(
             "TrackHeight",
             typeof(bool),
             typeof(ElementSizeMonitor),
             new PropertyMetadata(false, OnTrackHeightChanged));

    public static bool GetTrackHeight(DependencyObject obj)
    {
      return (bool)obj.GetValue(TrackHeightProperty);
    }

    public static void SetTrackHeight(DependencyObject obj, bool value)
    {
      obj.SetValue(TrackHeightProperty, value);
    }

    private static void OnTrackHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      if (d is FrameworkElement element && (bool)e.NewValue)
      {
        element.SizeChanged += (s, args) =>
        {
          SetActualHeight(element, args.NewSize.Height);
        };

        // 初始化
        SetActualHeight(element, element.ActualHeight);
      }
    }

    public static readonly DependencyProperty ActualHeightProperty =
        DependencyProperty.RegisterAttached(
            "ActualHeight",
            typeof(double),
            typeof(ElementSizeMonitor),
            new PropertyMetadata(0.0));

    public static double GetActualHeight(DependencyObject obj)
    {
      return (double)obj.GetValue(ActualHeightProperty);
    }

    public static void SetActualHeight(DependencyObject obj, double value)
    {
      obj.SetValue(ActualHeightProperty, value);
    }
  }
}
